package com.mdxq.mall.product.service.impl;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.mdxq.mall.product.entity.*;
import com.mdxq.mall.product.mapper.*;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.mdxq.mall.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

import static com.mdxq.mall.product.entity.table.ProductTableDef.PRODUCT;

/**
 * 商品信息 服务层实现。
 *
 * @author zdy
 * @since 2025-01-12
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>  implements ProductService{
    @Autowired
    private AlbumPicMapper albumPicMapper;//商品图册mapper
    @Autowired
    private AlbumMapper albumMapper;//商品图片mapper
    @Autowired
    private ProductAttributeValueMapper attrValueMapper;
    @Autowired
    private SkuStockMapper skuStockMapper;//sku库存mapper
    @Override
    @Transactional
    public boolean save(Product product) {
        // 1.保存商品图册
        Album album = new Album();
        album.setName(product.getName());//相册名称和商品名称一致
        album.setCoverPic(product.getPic());//相册封面和商品封面一致
        String[] albumPics = product.getAlbumPics().split(",");
        album.setPicCount(albumPics.length);
        albumMapper.insert(album);//保存商品图册

        // 2.保存图册中的图片
        for (String url : albumPics) {
            AlbumPic albumPic = new AlbumPic(null,album.getId(),url);
            albumPicMapper.insert(albumPic);
        }

        // 3.保存商品信息,数据库会将生成的商品id返回到实例中
        super.save(product);

        // 4.保存商品的属性值
        for (ProductAttributeValue attrValue : product.getProductAttributeValueList()) {
            attrValue.setProductId(product.getId());//注入商品id
            attrValueMapper.insert(attrValue);
        }

        // 5.保存sku库存
        for (SkuStock skuStock : product.getSkuStockList()) {
            skuStock.setProductId(product.getId());
        }
        skuStockMapper.insertBatch(product.getSkuStockList());
        return true;
    }

    @Override
    public Product getById(Serializable id) {
        Product product = super.getById(id);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("product_id",id);//条件
        //查询商品属性
        product.setProductAttributeValueList(attrValueMapper.selectListByQuery(queryWrapper));
        queryWrapper.clear();
        queryWrapper.eq("product_id",id);//条件
        //查询sku库存
        product.setSkuStockList(skuStockMapper.selectListByQuery(queryWrapper));

        return product;
    }

    @Override
    @Transactional
    public boolean updateById(Product product) {
        // 1.保存商品图册
        Album album = new Album();
        album.setName(product.getName());//相册名称和商品名称一致
        album.setCoverPic(product.getPic());//相册封面和商品封面一致
        String[] albumPics = product.getAlbumPics().split(",");
        album.setPicCount(albumPics.length);
        albumMapper.insert(album);//保存商品图册

        // 2.保存图册中的图片
        for (String url : albumPics) {
            AlbumPic albumPic = new AlbumPic(null,album.getId(),url);
            albumPicMapper.insert(albumPic);
        }

        // 3.保存商品信息,数据库会将生成的商品id返回到实例中
        mapper.update(product);

        // 4.保存商品的属性值
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("product_id",product.getId());
        attrValueMapper.deleteByQuery(queryWrapper);
        for (ProductAttributeValue attrValue : product.getProductAttributeValueList()) {
            attrValue.setProductId(product.getId());//注入商品id
            attrValueMapper.insert(attrValue);
        }

        // 5.保存sku库存
        queryWrapper.clear();
        queryWrapper.eq("product_id",product.getId());
        skuStockMapper.deleteByQuery(queryWrapper);
        for (SkuStock skuStock : product.getSkuStockList()) {
            skuStock.setProductId(product.getId());
        }
        skuStockMapper.insertBatch(product.getSkuStockList());
        return true;
    }

    @Override
    public boolean updateStock(Long id, Integer num) {
        Product product = UpdateEntity.of(Product.class,id);
        UpdateWrapper<Product> updateWrapper = UpdateWrapper.of(product);
//        updateWrapper.setRaw("stock","stock + " + num);也可以
//        updateWrapper.setRaw("stock",String.format("stock + %d",num));也可以
        updateWrapper.set(PRODUCT.STOCK,PRODUCT.STOCK.add(num));
        return mapper.update(product)>0;
    }
}
